﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using FinalProject.BL;

namespace FinalProject.UI
{
    /// <summary>
    /// Interaction logic for InvoicePage.xaml
    /// </summary>
    public partial class InvoicePage : UserControl, IBasePage
    {

        private DataGridTextColumn colItem,
                               colDescrip,
                               colWarehouse,
                               colQuantity,
                               colRemainQuantity,
                               colPrice,
                               colTotalAmount;

        private DataGridTextColumn
            colInvoiceId,
            colInvoiceJournal,
            colInvoiceNumber,
            colInvoiceDate,
            colInvoiceCustomer,
            colInvoiceTotal,
            colInvoiceCurrency,
            colInvoiceDescrip;

        public InvoicePage()
        {
            this.InitializeComponent();
            this.InitializeColumnGridDetail();
            this.InitializeColumnInvoiceList();
            this.InitializeEvents();
        }

        private void InitializeColumnGridDetail()
        {
            this.grvItem.ColumnHeaderHeight = 30;
            this.colItem = new DataGridTextColumn()
            {
                Header = "Item Name",
                Binding = new Binding("ItemName"),
                IsReadOnly = true,
                Width = 110
            };
            this.grvItem.Columns.Add(this.colItem);

            this.colWarehouse = new DataGridTextColumn()
            {
                Header = "Warehouse",
                Binding = new Binding("WarehouseName"),
                IsReadOnly = true,
                Width = 150

            };
            this.grvItem.Columns.Add(this.colWarehouse);

            this.colQuantity = new DataGridTextColumn()
            {
                Header = "Quantity",
                Binding = new Binding("Quantity"),
                IsReadOnly = false,
                Width = 150

            };
            this.grvItem.Columns.Add(this.colQuantity);

            this.colRemainQuantity = new DataGridTextColumn()
            {
                Header = "Remain Quantity",
                Binding = new Binding("RemainQuantity"),
                IsReadOnly = true,
                Width = 150
            };
            this.grvItem.Columns.Add(this.colRemainQuantity);

            this.colPrice = new DataGridTextColumn()
            {
                Header = "Price",
                Binding = new Binding("Price"),
                IsReadOnly = true,
                Width = 150
            };
            this.grvItem.Columns.Add(this.colPrice);

            this.colTotalAmount = new DataGridTextColumn()
            {
                Header = "Total",
                Binding = new Binding("TotalAmount"),
                IsReadOnly = true,
                Width = 150
            };
            this.grvItem.Columns.Add(this.colTotalAmount);

            this.colDescrip = new DataGridTextColumn()
            {
                Header = "Description",
                Binding = new Binding("Description"),
                IsReadOnly = true,
                Width = 500
            };
            this.grvItem.Columns.Add(this.colDescrip);
        }

        private void InitializeColumnInvoiceList()
        {
            this.grvInvoice.ColumnHeaderHeight = 30;
            this.colInvoiceId = new DataGridTextColumn()
            {
                Header = "Id",
                Visibility = System.Windows.Visibility.Hidden
            };
            this.grvInvoice.Columns.Add(this.colInvoiceId);

            this.colInvoiceJournal = new DataGridTextColumn()
            {
                Header = "Journal",
                Binding = new Binding("JournalCode"),
                Width = 100
            };
            this.grvInvoice.Columns.Add(this.colInvoiceJournal);

            this.colInvoiceNumber = new DataGridTextColumn()
            {
                Header = "Number",
                Binding = new Binding("DocNumber"),
                Width = 100
            };
            this.grvInvoice.Columns.Add(this.colInvoiceNumber);

            this.colInvoiceDate = new DataGridTextColumn()
            {
                Header = "Date",
                Binding = new Binding("DocDate"),
                Width = 150
            };
            this.grvInvoice.Columns.Add(this.colInvoiceDate);

            this.colInvoiceCustomer = new DataGridTextColumn()
            {
                Header = "Customer",
                Binding = new Binding("CustomerCode"),
                Width = 100
            };
            this.grvInvoice.Columns.Add(this.colInvoiceCustomer);

            this.colInvoiceTotal = new DataGridTextColumn()
            {
                Header = "Total amount",
                Binding = new Binding("TotalAmount"),
                Width = 80
            };
            this.grvInvoice.Columns.Add(this.colInvoiceTotal);

            this.colInvoiceCurrency = new DataGridTextColumn()
            {
                Header = "Currency",
                Binding = new Binding("Currency"),
                Width = 80
            };
            this.grvInvoice.Columns.Add(this.colInvoiceCurrency);

            this.colInvoiceDescrip = new DataGridTextColumn()
            {
                Header = "Description",
                Binding = new Binding("Description"),
                Width = 250
            };
            this.grvInvoice.Columns.Add(this.colInvoiceDescrip);
        }

        public void SaveData()
        {
        }

        public void CancelModified()
        {
        }

        public void ChangeToNewMode()
        {
            this.txtNumber.Text = InvoiceManagementBL.GetLastestNumber().ToString();

        }

        private void OpenDeliveryLookupForm()
        {
            LookupForm frmLookup = new LookupForm(typeof(DAL.Model.DeliveryDocument));
            frmLookup.FormSendSelected += frmLookup_FormSendSelected;
            frmLookup.ShowDialog();
        }

        private void frmLookup_FormSendSelected(object sender)
        {
            LookupForm frmLookup = sender as LookupForm;
            if (frmLookup == null) return;

            Guid deliId = frmLookup.SelectedId;
            if (deliId == Guid.Empty) return;

            DAL.Model.DeliveryDocument deliveryDoc = DeliveryManagementBL.LoadById(deliId);
            if (deliveryDoc != null)
            {
                this.lkuCustomer.SetSelectedValue(deliveryDoc.Customer);
                this.cmbPayMode.SelectedValue = deliveryDoc.OrderDocument.PayMode;

            }
        }

        private void lblSelectDeliveryDoc_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.OpenDeliveryLookupForm();
        }

        public void ChangeToNormalMode()
        {
        }

        #region Events
        private void InitializeEvents()
        {
            this.Loaded += InvoicePage_Loaded;
            this.lblSelectDeliveryDoc.PreviewMouseDown += lblSelectDeliveryDoc_PreviewMouseDown;
        }

        void lblSelectDeliveryDoc_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.OpenDeliveryLookupForm();
        }

        //void lblSelectDeliveryDoc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    this.OpenDeliveryLookupForm();
        //}

        void InvoicePage_Loaded(object sender, RoutedEventArgs e)
        {
            this.cmbJournal.GetAllItems();
        }

        #endregion
    }
}